package gazzera.utils;

import java.io.*;

public class FileIDUtils {

	public static void write(FileID id, DataOutput x) throws IOException
	{
		if(id instanceof Hash)
		{
			x.writeByte(0);
			x.writeUTF(id.toString());
		}
		else if(id instanceof UserFileID)
		{
			x.writeByte(1);
			x.writeUTF(((UserFileID)id).getUserHash());
			x.writeUTF(((UserFileID)id).getFilePath());
			x.writeLong(((UserFileID)id).getTimeStamp());
			x.writeLong(((UserFileID)id).getSize());
		}
		else
		{
			throw new IOException("Unrecognized FileID");
		}
	}
	public static FileID read(DataInput x) throws IOException
	{
		byte b = x.readByte();
		if(b == 0)
		{
			String s = x.readUTF();
			return new Hash(s);
		}
		else if(b==1)
		{
			String u = x.readUTF();
			String p = x.readUTF();
			long t = x.readLong();
			long s = x.readLong();
			return new UserFileID(u, p, t, s);
		}
		else
		{
			throw new IOException("Unrecognized FileID");
		}
	}
	
}
